/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistence;

import java.util.ArrayList;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

/**
 * This class is used to encapsulate the persistence operations
 * done in the database. It provides methods to select, insert,
 * update or find inside the database
 * 
 * @author Amigos do Noddy
 *
 */
public class Command {

    private static MyEntityManagerFactory myemf;
    
    public static Object select(String sql, ArrayList p) {
        try
        {
    	Object results = null;
        myemf = MyEntityManagerFactory.getInstance();
        EntityManager em = myemf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        try {
            Query query = em.createQuery(sql);
            for(int i = 0; i<p.size(); i++ )
                query.setParameter(i+1, p.get(i));
            
            results = query.getResultList();
            tx.commit();
        } catch (Exception e) {
        	e.printStackTrace();
            tx.rollback();
        } finally {
            em.close();
        }

        return results;
        }
        catch(Exception e)
        {
        	e.printStackTrace();
        }
        return null;
    }
    
    public static boolean set(Object o)
    {
    	try{
        boolean ret = false;
        myemf = MyEntityManagerFactory.getInstance();
        EntityManager em = myemf.createEntityManager();

        EntityTransaction tx = em.getTransaction();
        tx.begin();
        try {
            em.persist(o);
            tx.commit();
            ret = true;
        } catch (Exception e) {
        	e.printStackTrace();
        	tx.rollback();
        } finally {
            em.close();
        }
        
        return ret;
    	}catch(Exception e)
    	{
    		e.printStackTrace();
    	}
    	return false;
    }
    
    public static boolean update(Object o)
    {
    	try{
        boolean ret = false;
        myemf = MyEntityManagerFactory.getInstance();
        EntityManager em = myemf.createEntityManager();

        EntityTransaction tx = em.getTransaction();
        tx.begin();
        try {
            em.merge(o);
            tx.commit();
            ret = true;
        } catch (Exception e) {
        	e.printStackTrace();
        	tx.rollback();            
        } finally {
            em.close();
        }
        
        return ret;
    	}catch(Exception e)
    	{
    		e.printStackTrace();
    	}
    	return false;
    }
    
    public static Object find(Class c, Integer id)
    {
    	myemf = MyEntityManagerFactory.getInstance();
        EntityManager em = myemf.createEntityManager();
        Object o = null;
        try{
        	o = em.find(c, id);
        }catch(Exception e){
        	return null;
        }finally{
        	em.close();
        }
        return o;        
    }
    
}
